package com.coke.demo.controller;

import com.coke.demo.model.Record;
import com.coke.demo.model.Result;
import com.coke.demo.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19602
 */
@RestController
@RequestMapping("record")
public class RecordController {

    private RecordService recordService;
    private UserService userService;
    private LabService labService;
    private OpenDayService openDayService;
    private CourseTimeService courseTimeService;

    @Autowired
    public void setRecordService(RecordService recordService) {
        this.recordService = recordService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setLabService(LabService labService) {
        this.labService = labService;
    }

    @Autowired
    public void setOpenDayService(OpenDayService openDayService) {
        this.openDayService = openDayService;
    }

    @Autowired
    public void setCourseTimeService(CourseTimeService courseTimeService) {
        this.courseTimeService = courseTimeService;
    }

    /**
     * 查询所有订单信息
     *
     * @return Result
     */
    @ResponseBody
    @PostMapping("/findAllRecord")
    @CrossOrigin
    public Result findAllRecord() {
        List<Record> records = recordService.findAllRecord();
        if (records != null) {
            return new Result("200", "查询成功", records);
        } else {
            return new Result("500", "查询失败");
        }
    }

    /**
     * 根据id删除订单
     *
     * @param id 订单id
     * @return Result
     */
    @ResponseBody
    @DeleteMapping("/deleteRecordById")
    @CrossOrigin
    public Result deleteRecordById(Integer id) {
        if (recordService.deleteRecordById(id)) {
            return new Result("200", "删除成功");
        } else {
            return new Result("500", "删除失败");
        }
    }

    /**
     * 根据id删除多个订单
     *
     * @param ids 多个订单id
     * @return Result
     */
    @ResponseBody
    @DeleteMapping("/deleteRecordSome")
    @CrossOrigin
    public Result deleteRecordSome(Integer[] ids) {
        for (Integer id : ids) {
            recordService.deleteRecordById(id);
        }
        return new Result("200", "删除成功");
    }

    /**
     * 根据id查询订单信息
     *
     * @param recordid 订单id
     * @return Result
     */
    @ResponseBody
    @PostMapping("/findRecordById")
    @CrossOrigin
    public Result findRecordById(Integer recordid) {
        Record record = recordService.findRecordById(recordid);
        if (record != null) {
            return new Result("200", "查询成功", record);
        } else {
            return new Result("500", "查询失败");
        }
    }

    /**
     * 根据id修改订单信息
     *
     * @param record 修改后的订单信息
     * @return Result
     */
    @ResponseBody
    @PostMapping("/updateRecordById")
    @CrossOrigin
    public Result updateRecordById(Record record) {
        if (recordService.updateRecordById(record)) {
            return new Result("200", "修改成功");
        } else {
            return new Result("200", "修改失败");
        }
    }

    /**
     * 把预约信息存入数据库
     *
     * @param record 订单信息
     * @return Result
     */
    @ResponseBody
    @PostMapping("/insertRecord")
    @CrossOrigin
    public Result insertRecord(Record record) {
        //判断用户信息是否正确：学号与姓名、院系是否相匹配
        if (userService.findById(record.getUserid()).getUsername().equals(record.getUsername())
                && userService.findById(record.getUserid()).getFaculty().equals(record.getFaculty())) {
            //判断实验室信息是否正确：实验室id与实验室名称是否匹配
            if (labService.findLabById(record.getLabid()).getLabname().equals(record.getLabname())) {
                //判断实验室是公用 or 专用实验室是否是相关专业用户预约
                if (labService.findLabById(record.getLabid()).getRole() == 0
                        || labService.findLabById(record.getLabid()).getFaculty().equals(record.getFaculty())) {
                    //判断实验室容纳人数是否足够学生就坐
                    if (labService.findLabById(record.getLabid()).getSeats() >= record.getUserNum()) {
                        //判断用户预约的时间实验室是否开放
                        if (openDayService.findDayByLabId(record.getLabid()).contains(record.getIndex())
                                && courseTimeService.findTimeByLabId(record.getLabid()).contains(record.getTime())) {
                            //判断用户预约的时间是否与其他用户预约的时间冲突
                            if (!recordService.findAllRecordBookTime().contains(record.getBookTime())) {
                                if (recordService.insertRecord(record)) {
                                    return new Result("200", "预约成功");
                                } else {
                                    return new Result("500", "预约失败");
                                }
                            } else {
                                return new Result("500", "该时间段已有其他用户预约，请选择其他时间！");
                            }
                        } else {
                            return new Result("500", "该时间段此实验室不开放，请重新选择时间！");
                        }
                    } else {
                        return new Result("500", "该实验室座位不足");
                    }
                } else {
                    return new Result("500", "该实验室仅限相关专业使用");
                }
            } else {
                return new Result("404", "实验室未查到，请检查实验室id和名称");
            }
        } else {
            return new Result("404", "用户未找到，请检查用户id和名称");
        }
    }


    /**
     * 根据用户id查询用户预约记录
     *
     * @param userid 用户id
     * @return Result
     */
    @ResponseBody
    @PostMapping("/findUserRecord")
    @CrossOrigin
    public Result findUserRecord(Integer userid) {
        List<Record> records = recordService.findUserRecord(userid);
        if (records != null) {
            return new Result("200", "查询成功", records);
        } else {
            return new Result("500", "查询失败");
        }
    }

    /**
     * 根据实验室名称模糊查询订单信息
     *
     * @param labname 实验室名称
     * @return Result
     */
    @ResponseBody
    @PostMapping("/queryRecordByLabName")
    @CrossOrigin
    public Result queryRecordByLabName(String labname) {
        ArrayList<Record> records = recordService.queryRecordByLabName(labname);
        if (records != null) {
            return new Result("200", "查询成功", records);
        } else {
            return new Result("500", "查询失败");
        }
    }

}
